#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <vector>

using namespace std;

struct TreeNode {
     int val;
     TreeNode *left;
     TreeNode *right;
     TreeNode() : val(0), left(nullptr), right(nullptr) {}
     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution01 {
public:
    bool evaluateTree(TreeNode* root) {
        if (root->right == nullptr && root->left == nullptr)
            return root->val;
        bool left = evaluateTree(root->left);
        bool right = evaluateTree(root->right);
        return root->val == 2 ? left | right : left & right;
    }
};

class Solution02 {
public:
    int sumNumbers(TreeNode* root) {
        return dfs(root, 0);
    }

    int dfs(TreeNode* root, int presum) {
        int left = 0, right = 0;
        presum = presum * 10 + root->val;
        if (root->left == nullptr && root->right == nullptr)
            return presum;
        if (root->left)
            left = dfs(root->left, presum);
        if (root->right)
            right = dfs(root->right, presum);

        return left + right;
    }
};

class Solution03 {
public:
    TreeNode* pruneTree(TreeNode* root) {
        if (root == nullptr)
            return nullptr;
        root->left = pruneTree(root->left);
        root->right = pruneTree(root->right);
        if (root->val == 0 && root->left == nullptr && root->right == nullptr)
            return nullptr;
        else
            return root;
    }
};